package com.huang.app.demo2

import com.blankj.utilcode.util.ToastUtils
import com.huang.app.demo2.dialog.LoadingDialog
import com.huang.mvvm.activity.BaseActivity
import com.huang.mvvm.control.IControllable
import com.huang.mvvm.control.IController
import java.lang.ref.WeakReference

class Demo2Controllable : IControllable {

    private var loadingDialog: WeakReference<LoadingDialog>? = null

    override fun showLoading(
        controller: IController,
        isOutsideEnable: Boolean,
        isBackEnable: Boolean,
        onCanceledListener: (() -> Unit)?
    ) {
        loadingDialog = WeakReference(LoadingDialog.Builder()
            .setOutsideCancelable(isOutsideEnable)
            .setBackEnable(isBackEnable)
            .setDismissListener {
                onCanceledListener?.invoke()
            }
            .build()
        )
        loadingDialog?.get()?.show((controller as BaseActivity<*,*>).supportFragmentManager, "loading")
    }

    override fun hideLoading() {
        loadingDialog?.get()?.dismiss()
    }

    override fun showMessage(text: String) {
        ToastUtils.showShort(text)
    }

    override fun onDestroy() {
        hideLoading()
        loadingDialog?.clear()
    }
}